#!/bin/bash
###SHELLPACK preamble monitor 0
###SHELLPACK init_complete
###SHELLPACK monitor_hooks

EXITING=
cleanup()
{
	rm /tmp/mmtests-monitor.pid
	EXITING=yes
}
trap cleanup SIGTERM
trap cleanup SIGINT
trap cleanup EXIT

wait_time=${MONITOR_PROFILE_WAIT_TIME:-0}
if [ $wait_time -gt 0 ]; then
	echo Sleeping $wait_time seconds before start of profiling
	sleep $wait_time
fi
monitor_pre_hook $LOGDIR_RESULTS monitor
echo $$ > /tmp/mmtests-monitor.pid
if [ "$MONITOR_CMD" != "" ]; then
	echo Monitoring while running \"$MONITOR_CMD\"
	$MONITOR_CMD
elif [ "$MONITOR_FOREVER" = "yes" ]; then
	echo Run kill $$ or kill \`cat /tmp/mmtests-monitor.pid\` to finish monitoring.
	while [ "$EXITING" != "yes" ]; do
		sleep 60
	done
else
	echo Hit enter, run kill $$ or kill \`cat /tmp/mmtests-monitor.pid\` to finish monitoring.
	read INPUT
fi
echo Exiting, may take time to compress logs
monitor_post_hook $LOGDIR_RESULTS monitor

exit $SHELLPACK_SUCCESS
